let aaa="";
let tj=0;
$(function () {

    $(".gxmx").hide();
    $("#cpwlmx").hide();
    $("#table11").hide();

    $("#gxwlsjbg").bootstrapTable({
        method: "post",
        contentType: 'application/json',
        url: "/gxwl/gxwlsjdbg",
        striped: true,
        pageNumber: 1,
        pagination: true,
        sidePagination: 'server',
        pageSize: 3,
        pageList: [2, 3, 5, 6],
        queryParams: function (parmes) {
            return {
                pageNum: (parmes.offset / parmes.limit) + 1,
                pageSize: parmes.limit,
            }
        },
        columns: [
            {title: "工序设计单号", field: "designId", align: 'center'},
            {title: "产品编号", field: "productId", align: 'center'},
            {title: "产品名称", field: "productName", align: 'center'},
            {title: "设计人", field: "designer", align: 'center'},
            {title: "登记时间", field: "registerTime", align: 'center'},
            {title: "工时总成本", field: "costPriceSum", align: 'center'},
            {title: "变更", field: "id", align: 'center',
                formatter: function (value, row, index) {
                    let a="<a href='#' onclick='bg("+JSON.stringify(row)+")'>变更</a>";
                    return a;
                }
            },
        ]
    })
})

function bg(a) {
    tj=a.id;
    $("#zhudiv").hide();
    $(".gxmx").show();
    $("#cpwlmx").hide();
    $("#table11").hide();

    $("#name").html(a.productName);
    $("#bh").html(a.productId);
    $("[name=designer]").html(a.designer);
    $("#djtime").html(a.registerTime);
    $("#comment").html(a.procedureDescribe);
    $("#gxdbh").html(a.designId);
    $("#zcb").html(a.costPriceSum);
    $("#wlcb").html(a.moduleCostPriceSum);
    $("#djr").html(a.register);

    $.ajax({
        method: "post",
        contentType: 'application/json',
        url: "/gxwl/gxmxwlsj/" + a.id,
        dataType: "json",
        async: true,
        success: function (data) {
            let tab = document.getElementById("wlsjmx")
            for (let i = 0; i < data.length; i++) {
                /*aar[i] = data[i].id*/
                tab.innerHTML += "<tr><td>" + data[i].detailsNumber + "</td><td>" + data[i].procedureName + "</td><td  class='gxbh1'>" + data[i].procedureId + "</td>" +
                    "<td>" + data[i].labourHourAmount + "</td><td>" + data[i].subtotal + "</td><td class='wlxj'>" + data[i].moduleSubtotal + "</td>" +
                    "<td><a href='#' onclick='bg1(" + data[i].id + ")'>变更</a></td></tr>"
            }
        }
    })

}
let aaaa=[];
function bg1(a) {
    $("#cpwlmx").show();
    $(".gxmx").hide();

    aaa=a;

    let arr=[];

    $.ajax({
        method: "post",
        contentType: 'application/json',
        url: "/gxwl/cxcpmx/"+a,
        dataType:"json",
        async:true,
        success:function (data) {
            for (let i=0;i<data.length;i++){
                arr.push(data[i].amount);
                aaaa.push(data[i].id);
            }
        }
    })

    let id=$("#bh").html();
    $.ajax({
        method: "post",
        contentType: 'application/json',
        url: "/gxwl/cpgxwlsj/"+id,
        dataType:"json",
        async:true,
        success:function (data) {
            let tab1=document.getElementById("tabsj")
            tab1.innerHTML="";
            for (let i=0;i<data.length;i++){
                tab1.innerHTML+="<tr><td>"+data[i].detailsNumber+"</td><td>"+data[i].productName+"</td><td>"+data[i].productId+"</td>" +
                    "<td>"+data[i].productDescribe+"</td><td>"+data[i].amount+"</td><td>"+data[i].residualAmount+"</td>" +
                    "<td>"+data[i].amountUnit+"</td><td class='dj'>"+data[i].costPrice+"</td><td><input class='sl' value='"+arr[i]+"' type=\"number\"></td></tr>"
            }
        }
    })
}
function yl() {
    $("#cpwlmx").hide();
    $(".gxmx").show();

    let sl=$(".sl");
    let dj=$(".dj");
    let zj=0;
    for (let i=0;i<sl.length;i++){
        zj+=parseInt(parseInt(sl[i].value)*parseInt(dj[i].innerHTML));
    }

    let a={id:aaa,moduleSubtotal:zj}
    $.ajax({
        method: "put",
        contentType: 'application/json',
        url: "/gxwl/xg",
        data:JSON.stringify(a),
        async:true,
        success:function (data) {}
    })

    for (let i=0;i<aaaa.length;i++){
        let dx={id:aaaa[i],amount:sl[i].value}
        $.ajax({
            method: "put",
            contentType: 'application/json',
            url: "/gxwl/xgsl",
            data:JSON.stringify(dx),
            async:true,
            success:function (data) {}
        })
    }
    $.ajax({
        method: "post",
        contentType: 'application/json',
        url: "/gxwl/gxmxwlsj/" + tj,
        dataType: "json",
        async: true,
        success: function (data) {
            let tab = document.getElementById("wlsjmx");
            tab.innerHTML="";
            for (let i = 0; i < data.length; i++) {
                /*aar[i] = data[i].id*/
                tab.innerHTML += "<tr><td>" + data[i].detailsNumber + "</td><td>" + data[i].procedureName + "</td><td  class='gxbh1'>" + data[i].procedureId + "</td>" +
                    "<td>" + data[i].labourHourAmount + "</td><td>" + data[i].subtotal + "</td><td class='wlxj'>" + data[i].moduleSubtotal + "</td>" +
                    "<td><a href='#' onclick='bg1(" + data[i].id + ")'>变更</a></td></tr>"
            }
        }
    })


}

function qdsd() {
    $.ajax({
        method: "put",
        contentType: 'application/json',
        url: "/gxwl/xgzt/"+tj,
        async:true,
        success:function (data) {
            returnList();
        }
    })
}

//返回列表
function returnList(){
    parent.document.getElementById('iframeId').contentWindow.location.reload(true);
}

//小返回
function fanhui(){
    $("#cpwlmx").hide();
    $(".gxmx").show();
}